<?php

namespace app\web\controller;
use app\web\model\OrderItemsModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 销量统计
 */
class ProEnterController extends StoreBaseController
{
 	protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new OrderItemsModel();
    }
    /**
     * 销量统计
     * @author [lx] 
     */
    public function index( $page = 1){
    		$agent_id = session('storeInfo')['id'];
    		$now=date('Y-m-d',strtotime('+1 day'));
		$befor=date('Y-m-d',strtotime('-7 day'));
    	if($page == 1){
    
		//优惠购销量统计
		$ysum = Db::name("mall_order_item")
		->where('agent_id',$agent_id)->whereTime('add_time', 'between', [$befor, $now])->where('goods_lanmu','1')
		->field('goods_name,sum(number) p')->group('goods_name')->order('p DESC')->limit(10)->select();
        $this->assign('ysum',$ysum);
		
		//疯狂抢销量统计
		$fsum = Db::name("mall_order_item")
		->where('agent_id',$agent_id)->whereTime('add_time', 'between', [$befor, $now])->where('goods_lanmu','2')
		->field('goods_name,sum(number) p')->group('goods_name')->order('p DESC')->limit(10)->select();
        $this->assign('fsum',$fsum);
		
		//赚积分销量统计
		$zsum = Db::name("mall_order_item")
		->where('agent_id',$agent_id)->whereTime('add_time', 'between', [$befor, $now])->where('goods_lanmu','3')
		->field('goods_name,sum(number) p')->group('goods_name')->order('p DESC')->limit(10)->select();
        $this->assign('zsum',$zsum);
		
		//云拼团销量统计
		$psum = Db::name("mall_order_item")
		->where('agent_id',$agent_id)->whereTime('add_time', 'between', [$befor, $now])->where('goods_lanmu','4')
		->field('goods_name,sum(number) p')->group('goods_name')->order('p DESC')->limit(10)->select();
        $this->assign('psum',$psum);
         return $this->fetch('index');
       }
       if($page == 2){
       	$sum = Db::name("line_order")->alias('a')
       	->join('line_orderitems b','a.id=b.order_id','LEFT')
		->where('a.agent_id',$agent_id)->whereTime('b.add_time', 'between', [$befor, $now])
		->field('b.name,sum(b.num) p')->group('b.name')->order('p DESC')->limit(10)->select();
        $this->assign('sum',$sum);
       		 return $this->fetch('index2');
       }
       
    }


   
}